home *** CD-ROM | disk | FTP | other *** search
/ 3D Images / 3D Images.iso / programs / amiga / rayshade / libshade / symtab.c < prev    next >
C/C++ Source or Header  |  1995-01-12  |  4KB  |  164 lines

  1. /*
  2.  * symtab.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb, Rod G. Bogart
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  * 
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * symtab.c,v 4.1 1994/08/09 08:05:06 explorer Exp
  17.  *
  18.  * symtab.c,v
  19.  * Revision 4.1  1994/08/09  08:05:06  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:19  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0.1.1  91/10/05  18:24:29  cek
  26.  * patch1: Added casts for builtin functions.
  27.  * 
  28.  * Revision 4.0  91/07/17  14:48:02  kolb
  29.  * Initial version.
  30.  * 
  31.  */
  32.  
  33. #include "rayshade.h"
  34. #include "symtab.h"
  35. #include "builtin.h"
  36. #ifdef AMIGA
  37. #ifndef __GNUC__
  38. extern double hypot(double, double);
  39. #endif
  40. #endif
  41.  
  42. static struct SymtabPredefinedEntry SymtabPredefined[] = {
  43.     {"pi", 3.141592, NULL, FLOAT_EXPR, FALSE, 0},
  44.     {"dtor", 0.017453, NULL, FLOAT_EXPR, FALSE, 0},
  45.     {"rtod", 57.29578, NULL, FLOAT_EXPR, FALSE, 0},
  46.     {"cos", 0.0, (Float (*)())cos, BUILTIN_EXPR, FALSE, 1},
  47.     {"sin", 0.0, (Float (*)())sin, BUILTIN_EXPR, FALSE, 1},
  48.     {"tan", 0.0, (Float (*)())tan, BUILTIN_EXPR, FALSE, 1},
  49.     {"sqrt", 0.0, (Float (*)())sqrt, BUILTIN_EXPR, FALSE, 1},
  50.     {"acos", 0.0, (Float (*)())acos, BUILTIN_EXPR, FALSE, 1},
  51.     {"asin", 0.0, (Float (*)())asin, BUILTIN_EXPR, FALSE, 1},
  52.     {"atan", 0.0, (Float (*)())atan, BUILTIN_EXPR, FALSE, 1},
  53.     {"hypot", 0.0, (Float (*)())hypot, BUILTIN_EXPR, FALSE, 2},
  54.     {"time", 0.0, NULL, FLOAT_EXPR, TRUE, 0},
  55.     {"frame", 0.0, NULL, FLOAT_EXPR, TRUE, 0},
  56.     {"linear", 0.0, LinearTime, BUILTIN_EXPR, TRUE, 4},
  57.     {NULL, 0.0, NULL, 0, 0}
  58. };
  59.  
  60. SymtabEntry *Symtab = (SymtabEntry *) NULL;
  61.  
  62. void SymtabAddEntry();
  63.  
  64. void
  65. SymtabInit()
  66. {
  67.     int i;
  68.  
  69.     for(i=0; SymtabPredefined[i].name; i++) {
  70.         if (SymtabPredefined[i].type == BUILTIN_EXPR)
  71.             SymtabAddEntry(SymtabPredefined[i].name,
  72.                        SymtabPredefined[i].type,
  73.                        NULL,
  74.                        SymtabPredefined[i].fp,
  75.                     SymtabPredefined[i].timevary,    
  76.                        SymtabPredefined[i].params);
  77.         else
  78.             SymtabAddEntry(SymtabPredefined[i].name,
  79.                        SymtabPredefined[i].type,
  80.                        ExprFloatCreate(SymtabPredefined[i].f,
  81.                         SymtabPredefined[i].timevary),
  82.                        NULL, SymtabPredefined[i].timevary, 0);
  83.     }
  84.     TimeExpr = ExprFloatSymtabFind("time");
  85.     FrameExpr = ExprFloatSymtabFind("frame");
  86. }
  87.  
  88. void
  89. SymtabAddEntry(name, type, expr, fp, timevary, params)
  90. char *name;
  91. Expr *expr;
  92. Float (*fp)();
  93. int type, timevary, params;
  94. {
  95.     SymtabEntry *res;
  96.  
  97.     if (SymtabFind(name) != (SymtabEntry *)NULL)
  98.         RLerror(RL_ABORT, "Symbol %s redefined.\n", name);
  99.  
  100.     res = (SymtabEntry *) Malloc( sizeof(SymtabEntry));
  101.     res->name = strsave(name);
  102.     res->type = type;
  103.     res->timevary = timevary;
  104.     switch (type) {
  105.     case FLOAT_EXPR:
  106.         res->value.expr = expr;
  107.         expr->symtab = TRUE;
  108.         break;
  109.     case BUILTIN_EXPR:
  110.         res->value.fp = fp;
  111.         break;
  112.     default:
  113.         RLerror(RL_WARN,
  114.             "Type %d not implemented!!!",type);
  115.     }
  116.     res->params = params;
  117.     res->next = Symtab;
  118.     Symtab = res;
  119. }
  120.  
  121. SymtabEntry *
  122. SymtabFind(name)
  123. char *name;
  124. {
  125.     SymtabEntry *res;
  126.     for(res=Symtab; res; res=res->next) {
  127.         if (!strcmp(res->name,name))
  128.             return res;
  129.     }
  130.     /*error*/
  131.     return NULL;
  132. }
  133.  
  134. Expr *
  135. ExprFloatSymtabFind(name)
  136. char *name;
  137. {
  138.     SymtabEntry *res;
  139.  
  140.     if ((res = SymtabFind(name)) == NULL) 
  141.         RLerror(RL_PANIC,
  142.             "Symbol %s not defined!\n", name);
  143.     if (res->type != FLOAT_EXPR)
  144.         RLerror(RL_PANIC,
  145.             "Symbol %s is not a float expression!\n", name);
  146.     return res->value.expr;
  147. }
  148.  
  149.  
  150. SymtabEntry *
  151. SymtabBuiltinFind(name)
  152. char *name;
  153. {
  154.     SymtabEntry *res;
  155.  
  156.     if ((res = SymtabFind(name)) == NULL) 
  157.         RLerror(RL_PANIC,
  158.             "Symbol %s not defined!\n", name);
  159.     if (res->type != BUILTIN_EXPR)
  160.         RLerror(RL_PANIC,
  161.             "Symbol %s is not a built in function!\n", name);
  162.     return res;
  163. }
  164.